<template>
  <el-form
    ref="form"
    class="edit-main-box"
    :disabled="disabled"
    :model="formData"
    :rules="rules"
    label-position="right"
    label-width="100px"
  >
    <el-form-item label="标题" prop="title">
      <el-input v-model="formData.title" placeholder="请输入" maxlength="50" />
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-radio v-model="formData.type" label="img" @change="imgs2 = []"
        >图片</el-radio
      >
      <el-radio v-model="formData.type" label="video" @change="imgs3 = []"
        >视频</el-radio
      >
      <el-radio
        v-model="formData.type"
        label="link"
        @change="
          () => {
            imgs2 = [];
            imgs3 = ''
          }
        "
        >链接</el-radio
      >
    </el-form-item>
    <el-row>
      <el-col :span="12">
        <el-form-item label="封面" prop="banner">
          <Upload
            :disabled="disabled"
            :file-list.sync="imgs"
            :limit="1"
            dir-name="/detail"
            list-type="img"
            @change="setUrl(formData, 'imgs', 'banner')"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item
          label="大图"
          v-if="formData.type === 'img'"
          prop="cover_pic"
        >
          <Upload
            :disabled="disabled"
            :file-list.sync="imgs2"
            :limit="1"
            dir-name="/detail"
            list-type="img"
            @change="setUrl(formData, 'imgs2', 'img')"
          />
        </el-form-item>
        <el-form-item
          label="视频"
          v-if="formData.type === 'video'"
          prop="cover_pic"
        >  <el-input v-model="formData.img3" placeholder="请输入视频地址"  />
         <!--  <Upload
            :disabled="disabled"
            :file-list.sync="imgs3"
            :limit="1"
            dir-name="/detail"
            list-type="video"
            @change="setUrl(formData, 'imgs3', 'img')"
          /> -->
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-form-item>
        <el-form-item label="链接" prop="link" v-if="formData.type === 'link'">
          <el-input v-model="formData.link" placeholder="请输入" />
        </el-form-item>
      </el-form-item>
    </el-row>
  </el-form>
</template>

<script>
import Upload from "@/components/Upload";
import FormMixin from "@/views/product/form-mixin.js";
export default {
  name: "AddDownload",
  mixins: [FormMixin],
  components: { Upload },
  props: {
    /** 类型  14新闻 15下载 16媒体 */
    type: {
      type: String,
      default: "14",
    },
    value: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      imgs: [],
      imgs2: [],
      imgs3: '',
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  computed: {
    formData: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
  },
  watch: {
    value: {
      handler(val) {
        this.imgs = val.img ? [{ save_url: val.img }] : [];
        this.imgs2 = val.img ? [{ save_url: val.link }] : [];
        this.imgs3 =  val.link ? [{ save_url: val.link }] : [];
      },
      immediate: true,
    },
  },
  methods: {
    setUrl(row, imgsField, field) {
      if (!this[imgsField].length) return;
      this.$set(row, field || "url", this[imgsField][0].save_url);
    },
    validate() {
      return new Promise(async (resolve, reject) => {
        try {
          await this.$refs.form.validate();
          resolve(this.formData);
        } catch (error) {
          reject(error);
        }
      });
    },
  },
};
</script>

<style lang="scss">
.edit-main-box {
  .add-box {
    background-color: #2d82f8;
  }

  .el-icon-circle-plus {
    font-size: 26px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 10px;
    cursor: pointer;
    color: #409eff;
  }
}
</style>
